package com.iplant.aps.controller.aps;

import com.iplant.aps.service.APSService;
import com.iplant.aps.service.ASVService;
import com.iplant.aps.service.po.aps.APSSchedulingVersionBPM;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/APSSchedulingVersionBPM")
public class APSSchedulingVersionBPMController extends BasicController {
    private static final Logger logger = LoggerFactory.getLogger(APSSchedulingVersionBPMController.class);

    public APSSchedulingVersionBPMController() {
        super(APSSchedulingVersionBPM.class);
    }

    @Autowired
    ASVService mASVService;

    /**
     * 查询任务（网页版）
     */
    @GetMapping("/EmployeeAllWeb")
    public Object EmployeeAllWeb(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);

            if (!wParams.containsKey("Status")) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            Pagination wPagination = GetPagination(request);

            ServiceResult<List<APSSchedulingVersionBPM>> wServiceResult =
                    mASVService.BPM_QueryEmployeeAllWeb(wLoginUser, wParams, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    /**
     * 查询单条
     */
    @Override
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            Map<String, Object> wParams = GetParamsMap(request);

            ServiceResult<APSSchedulingVersionBPM> wServiceResult =
                    mASVService.APS_QuerySchedulingVersionBPMInfo(wLoginUser, wParams);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
